package cn.xuewei.controller;


import cn.xuewei.mapper.QuestionMapper;
import cn.xuewei.pojo.vo.QuestionErrorVO;
import cn.xuewei.pojo.vo.QuestionVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/v1/question/")
public class QuestionController {
    @Autowired(required = false)
    private QuestionMapper questionMapper;

    @GetMapping("select")
    public List<QuestionVO> selectAll(){
        List<QuestionVO> question = new ArrayList<>();
//        List<QuestionVO> list = questionMapper.select();
        for (int i = 0; i < 10; i++) {
            int index = (int) ((Math.random()*240)+1);
            question.add(questionMapper.selectById(index));
        }
        return question;
    }
    @PostMapping("{username}/insert")//加入错题
    public void insertQuestion(@RequestBody String str,@PathVariable String username) throws JsonProcessingException {
        System.out.println(username);
        // System.out.println(str);
        ObjectMapper mapper = new ObjectMapper();
        JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, QuestionErrorVO.class);
        List<QuestionErrorVO> list = mapper.readValue(str, javaType);


//                if(questionMapper.selectByUsername(username)==null){
//             for (int i = 0; i < list.size(); i++) {
//                questionMapper.insert(list.get(i));
//            }
//       }

        int d = 0;
        List<QuestionErrorVO> errorVOList = questionMapper.selectByUsername(username);
        for (int i = 0; i < list.size(); i++) {
            int j = 0;
            for (int i1 = 0; i1 < errorVOList.size(); i1++) {
                if (list.get(i).getQuestionId() == errorVOList.get(i1).getQuestionId()) {
                    j++;
                }
            }
            if (j == 0) {
                questionMapper.insert(list.get(i));
            }

        }

    }
    @GetMapping("{username}/selectById")
    public List<QuestionVO> selectById(@PathVariable String username){

        List<QuestionErrorVO> errorVOList = questionMapper.selectByUsername(username);
        List<QuestionVO> list = new ArrayList<>();
        for (int i = 0; i < errorVOList.size(); i++) {
            list.add(questionMapper.selectById(errorVOList.get(i).getQuestionId()));

        }
        return list;
        }
    @PostMapping("{username}/delete")
    public void deleteQuestion(@RequestBody String str,@PathVariable String username) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, QuestionErrorVO.class);
        List<QuestionErrorVO> list = mapper.readValue(str, javaType);

        for (int i = 0; i < list.size(); i++) {
            questionMapper.delete(username,list.get(i).getQuestionId());

        }

    }
        }


    





